package com.changgou.goods.service.impl;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.dao.BrandMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;    //如果出现红线则需要在BrandMapper接口中加上@Repository注解

    //查询所有品牌
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    //通过id查询品牌
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    //新增品牌
    @Override
    @Transactional  //增删改需要添加事务
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    //修改品牌数据
    @Override
    @Transactional
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);  //传过来什么字段修改什么字段，没有传递的话  其他字段置为null
        // brandMapper.updateByPrimaryKeySelective(brand); //传过来什么字段修改什么字段，没有传递的话 其他字段保留原值
    }

    //删除品牌
    @Override
    @Transactional
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //分页查询
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        //设置查询条件
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //品牌名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            //品牌首字母
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    //品牌列表分页查询
    @Override
    public Page<Brand> findPage(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    //多条件分页查询
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页查询
        PageHelper.startPage(page, size);
        //设置查询条件
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //设置品牌名称的模糊查询
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            //设置品牌首字母的精确查询
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        return (Page<Brand>) brandMapper.selectByExample(example);
    }

    //根据商品分类名称查询品牌列表
    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }

    //根据商品分类名称查询规格
    @Override
    public List<Map> findSpecListCategoryName(String categoryName) {
        List<Map> specList = brandMapper.findSpecListByCategoryName(categoryName);
        for (Map map : specList) {
            String[] options = ((String) map.get("options")).split(",");
            map.put("options",options);
        }
        return specList;
    }
}
